package com.example.googlemapdrawroutelast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

public class GetIdUserClass extends AsyncTask<String, Void, ApplicationData>{

	private ApplicationData appData;
	boolean isGetIdUser = false;
	public AsyncResponse delegate = null;

	public GetIdUserClass(ApplicationData appData) {
		super();
		this.appData = appData;
	}

	@Override
	protected ApplicationData doInBackground(String... params) {
		try {
			String strUrl = "http://" + UrlClass.getUrl() + "/v1/usersEmail/"
					+ params[0].toString();
			URL url = new URL(strUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setConnectTimeout(15000);
			connection.setReadTimeout(15000);
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);

			InputStream iStream = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					iStream));

			StringBuffer sb = new StringBuffer();
			String line = "";

			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}

			reader.close();
			iStream.close();

			String jsonResponseString = sb.toString();
			JSONObject jsonResponse = new JSONObject(jsonResponseString);
			UserInfo userInfo = new UserInfo(jsonResponse.getInt("id"));
			int userId = userInfo.getId_user();

			// ApplicationData appData = new ApplicationData(userId);
			appData.setIdUser(userId);
			

			if (appData != null) {
				
				return appData;

			}

			/*
			 * if(this.userData.getIdUser() != null &&
			 * this.userData.getIdUser().isEmpty()) { isGetIdUser = true; }
			 */


		} catch (Exception e) {
			e.getMessage();
		}

		return appData;
	}

	protected void onPostExecute(ApplicationData result) {
		//this.appData.setIdUser(result);
		//delegate.processFinish(result);

	}
	
	 @Override
	    protected void onPreExecute() {
	        // TODO Auto-generated method stub
	        super.onPreExecute();
	    }

}
